import time
import math
import random
import pickle
from pprint import pprint
from operator import attrgetter

import pygame

import domination
from utilities import *

## Shortcuts

rand = random.random

if False:
    s = time.clock()
    for i in xrange(100000000):
        a,b,c,d,e = 1,2,3,4,5
    print "Tuple unpacking: %.3f"% (time.clock() - s)

    s = time.clock()
    for i in xrange(100000000):
        a = 1
        b = 2
        c = 3
        d = 4
        e = 5
    print "Variable assignment: %.3f"% (time.clock() - s)

if False:
    """ Testing to see whether sorting nested tuples is slower.
    """
    N = 1000000
    tuples = [(random.random(), random.random(), random.random()) for i in xrange(N)]
    nested = [((random.random(), random.random()), random.random()) for i in xrange(N)]

    s = time.clock()
    tuples.sort()
    print "Sorting tuples: %.3f"% (time.clock() - s)

    s = time.clock()
    nested.sort()
    print "Sorting nested tuples: %.3f"% (time.clock() - s)
    
if False:
    class T(object):
        def __init__(self):
            self.x = random.random()
    
    N = 1000000
    
    l1 = [T() for i in xrange(N)]
    l2 = [T() for i in xrange(N)]
    
    s = time.clock()
    l1.sort(key=lambda t: t.x)
    print "Using lambda key: %.3f"% (time.clock() - s)
    
    s = time.clock()
    l2.sort(key=attrgetter('x'))
    print "Using attrgetter key: %.3f"% (time.clock() - s)
    
if False:
    N = 1000000
    m = 1000
    
    
    d = {}
    s = time.clock()
    for _ in xrange(N):
        idx = random.randint(0,m)
        try:
            d[idx] = d[idx] + 1
        except KeyError:
            d[idx] = 1
    print "Using KeyError: %.3f"% (time.clock() - s)
    
    d = {}
    s = time.clock()
    for _ in xrange(N):
        idx = random.randint(0,m)
        d[idx] = d.get(idx,0) + 1   
    print "Using .get: %.3f"% (time.clock() - s)
    
    d = {}
    s = time.clock()
    for _ in xrange(N):
        idx = random.randint(0,m)
        if idx not in d:
            d[idx] = 0
        d[idx] = d[idx] + 1
    print "Using 'if key not in dict': %.3f"% (time.clock() - s)
    
    
if False:
    class Test(object):
        def __init__(self, some_list=[]):
            self.some_list = some_list
    
    a = Test()
    b = Test()
    a.some_list.append("apples")
    b.some_list.append("strawberries")
    print a.some_list # Prints ['apples', 'strawberries']
    print b.some_list # Prints ['apples', 'strawberries']
    
        
if False:
    ## TEST LINE GRID INTERSECTS
    pygame.init()
    screen = pygame.display.set_mode((400,400))
    
    grid = [[(1 if (rand() < 0.2) else 0) for i in xrange(20)] for j in xrange(20)]
    pprint(grid)
    
    for i in xrange(10000):
        if i%20 == 0:
            screen.fill((255,255,255))
            for i,row in enumerate(grid):
                for j,tile in enumerate(row):
                    if tile:
                        pygame.draw.rect(screen,(0,0,0),(j*20,i*20,20,20))
                    else:
                        pygame.draw.rect(screen,(100,100,100),(j*20,i*20,20,20),1)
        pygame.display.flip()
        time.sleep(1)
        p0 = random.randint(0,399),random.randint(0,399)
        p1 = random.randint(0,399),random.randint(0,399)
        isects = line_intersects_grid(p0,p1,grid,20)
        col = (255,0,0) if isects else (0,255,0)
        pygame.draw.line(screen,col,p0,p1,2)
        
if False:
    r = pickle.load(open('replay-field-disconnected'))
    f = r.field
    all_objects = f.get_objects()[0]
    add_points = [(o.cx, o.cy) for o in all_objects if (isinstance(o,domination.Ammo) or isinstance(o,domination.ControlPoint))]
    f.mesh = make_nav_mesh(f.wallrects, (0,0,f.width*f.tilesize,f.height*f.tilesize),
                                simplify=0.3, additional_points=add_points)
    g = domination.Game(field=f)
    g.run()

if True:
    print '%r'%domination.Settings()
    print '%r'%domination.Game()